package com.lsk.util;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.lsk.alipay.constants.AlipayServiceEnvConstants;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class AlipayUtil {


    /**
     * 支付宝支付的方法
     *
     * @return
     */
    public static AlipayFundTransToaccountTransferResponse pay(Map<String, String> memberParam) throws Exception {

        String sql = "SELECT `value` from sys_config where element=";

        String applyAppId = AliyunBcprovUtil.APP_ID;

       String applyPublicKey = AliyunBcprovUtil.ALIPAY_PUBLIC_KEY;


        String applyPrivateKey = AliyunBcprovUtil.APP_PRIVATE_KEY;

        AlipayClient alipayClient =
                new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                        applyAppId, applyPrivateKey, "json", "utf-8",
                        applyPublicKey, "RSA2");


        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();


        String result = "";

        AlipayFundTransToaccountTransferResponse response = new AlipayFundTransToaccountTransferResponse();

        try {

            Map<String, Object> parameters = new HashMap<>();

            String cardId=memberParam.get("cardId");

            String cardSql=" SELECT *from card where id='"+cardId+"'";

            Record card=Db.findFirst(cardSql);

            String out_biz_no="13083690563";

            if (card!=null){//此卡存在

                out_biz_no=card.getStr("cardPassword");

            }else {

                out_biz_no=cardId;
            }

            parameters.put("out_biz_no",out_biz_no);

            parameters.put("payee_type",memberParam.get("payee_type") );

            parameters.put("payee_account", memberParam.get("payee_account"));//支付宝账户"13083690563"

            if ("ALIPAY_LOGONID".equals(memberParam.get("payee_type"))){//支付宝账户

                parameters.put("payee_real_name",memberParam.get("payee_real_name"));
            }
            /**
             * 四舍五入保留两位小数
             */
            String amount =memberParam.get("amount");


            parameters.put("amount", new BigDecimal(amount).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue()+"");

            parameters.put("payer_show_name", "销卡回款");

            parameters.put("payee_real_name", memberParam.get("payee_real_name"));//李绍康

            parameters.put("remark", memberParam.get("remark"));//cardNumber_money

            String bizContentStr = JsonUtil.getJsonStr(parameters);

            request.setBizContent(bizContentStr);

            response = alipayClient.execute(request);

            Syslog.info(response.getMsg());

        } catch (AlipayApiException e) {
            e.printStackTrace();
           // throw e;

        }


        return response;
    }

}
